package com.crocodileQ.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crocodileQ.common.PageResult;
import com.crocodileQ.common.R;
import com.crocodileQ.constants.SystemConstants;
import com.crocodileQ.domain.VO.ArticleDetailVO;
import com.crocodileQ.domain.VO.ArticleListVO;
import com.crocodileQ.domain.VO.HotArticleVO;
import com.crocodileQ.domain.entity.Article;
import com.crocodileQ.domain.entity.Category;
import com.crocodileQ.mapper.ArticleMapper;
import com.crocodileQ.service.ArticleService;
import com.crocodileQ.service.CategoryService;
import com.crocodileQ.utils.BeanCopyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private CategoryService categoryService;


    /**
     * 查询热门文章
     * @return
     */
    public R<List<HotArticleVO>> hotArticleList() {

        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        //必须是正式文章
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        //必须是未删除的
        queryWrapper.eq(Article::getDelFlag,SystemConstants.NOT_DEL);
        //按照浏览量，倒序排序
        Page<Article> page = new Page<>(1,10);
        page(page,queryWrapper);

        List<Article> articleList = page.getRecords();
        articleList.stream()
                .map(article -> article.setCategoryName(categoryService.getById(article.getCategoryId()).getName()))
                .collect(Collectors.toList());

        //转化成vo返回
        List<HotArticleVO> hotArticleVOS = BeanCopyUtil.copyBeanList(articleList, HotArticleVO.class);


        return R.success(hotArticleVOS);
    }

    /**
     * 分页查询文章
     * @return
     */
    public R<PageResult<ArticleListVO>> articleList(Integer pageNum, Integer pageSize, Long categoryId,String name) {
        //查询条件
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        //如果有categoryId,查询时判断
        queryWrapper.eq(Objects.nonNull(categoryId)&&categoryId>0,Article::getCategoryId,categoryId);
        queryWrapper.like(StringUtils.hasText(name),Article::getTitle,name);
        //状态是正式发布 0
        queryWrapper.eq(Article::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL);
        //必须是未删除的
        queryWrapper.eq(Article::getDelFlag,SystemConstants.NOT_DEL);
        //对isTop进行降序排序
        queryWrapper.orderByDesc(Article::getIsTop);

        //分页查询
        Page<Article> page = new Page<>(pageNum, pageSize);
        page(page,queryWrapper);

        //查询所属分类名称
        List<Article> articleList = page.getRecords();
//        for (Article article : articleList) {
//            Category category = categoryService.getById(article.getCategoryId());
//            article.setCategoryName(category.getName());
//        }
        //lambda编程
        articleList.stream()
                .map(article -> article.setCategoryName(categoryService.getById(article.getCategoryId()).getName()))
                .collect(Collectors.toList());

        //封装articleListVO
        List<ArticleListVO> articleListVOS = BeanCopyUtil.copyBeanList(page.getRecords(), ArticleListVO.class);
        //封装成PageResult
        PageResult<ArticleListVO> pageResult = new PageResult<>(articleListVOS,page.getTotal());

        return R.success(pageResult);
    }

    /**
     * 获取文章详情
     * @param id
     * @return
     */
    public R<ArticleDetailVO> getArticleDetail(Long id) {
        //根据id查询文章
        Article article = getById(id);
        //转换成VO
        ArticleDetailVO articleDetailVO = BeanCopyUtil.copyBean(article, ArticleDetailVO.class);

        //根据分类id查询分类名
        String categoryName = categoryService.getById(articleDetailVO.getCategoryId()).getName();
        articleDetailVO.setCategoryName(categoryName);

        //封装响应返回
        return R.success(articleDetailVO);
    }
}
